package com.futures.investm.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.futures.investm.R;
import com.futures.investm.adapter.CalendarListAdapter;
import com.futures.investm.adapter.OnLineServiceListAdapter;
import com.futures.investm.common.LoginActivity;
import com.futures.investm.constant.Api;
import com.futures.investm.entity.CalendarItem;
import com.futures.investm.entity.OnLineServiceMsg;
import com.futures.investm.listener.OnNoDoubleClickListener;
import com.futures.investm.utils.CacheData;
import com.futures.investm.utils.DateUtil;
import com.futures.investm.utils.DisplayUtils;
import com.aries.ui.view.radius.RadiusTextView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.http.request.GetRequest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import tech.com.commoncore.app.FastManager;
import tech.com.commoncore.base.BaseFragment;
import tech.com.commoncore.base.BaseRefreshLoadFragment;
import tech.com.commoncore.constant.BussConstant;
import tech.com.commoncore.entity.BaseTResp;
import tech.com.commoncore.manager.LoggerManager;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.ToastUtil;

public class DiscussFragment extends BaseRefreshLoadFragment<CalendarItem> {
//    @BindView(R.id.recyclerView)
//    RecyclerView recyclerView;
//    @BindView(R.id.et_content)
//    EditText etContent;
//    @BindView(R.id.tv_send)
//    RadiusTextView tvSend;
//
//    private OnLineServiceListAdapter mAdapter;
//    private AVUser mUser;
//
//    protected int mDefaultPage = 0;
//    protected int mDefaultPageSize = 10;

    @BindView(R.id.tv_date)
    TextView tvDate;



    public static DiscussFragment newInstance(){
        DiscussFragment fragment = new DiscussFragment();

        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_discuss;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tvDate.setText(DateUtil.formatDate(new Date()));
//            mUser = CacheData.getLoginUser();
//
//        mAdapter = new OnLineServiceListAdapter();
//        //设置上拉加载
//        mAdapter.setUpFetchEnable(true);
//
//        mAdapter.setStartUpFetchPosition(2);
//        mAdapter.setUpFetchListener(new BaseQuickAdapter.UpFetchListener() {
//            @Override
//            public void onUpFetch() {
//                if (mUser != null) {
//                    startUpFetch();
//                }
//            }
//        });
//
//        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
//        recyclerView.setAdapter(mAdapter);
//
//        if (mUser != null) {
//            loadData(0);
//        }
//
//        tvSend.setOnClickListener(new OnNoDoubleClickListener() {
//            @Override
//            public void onNoDoubleClick(View v) {
//                if (mUser != null) {
//                    String content = etContent.getText().toString().trim();
//                    if (DataUtils.isEmptyString(content)) {
//                        ToastUtil.show("亲,您还没输入内容...");
//                        return;
//                    }
//                    sendMessage(content);
//                }else {
//                    FastUtil.startActivity(mContext, LoginActivity.class);
//                }
//
//
//
//            }
//        });
    }

    @Override
    public BaseQuickAdapter<CalendarItem, BaseViewHolder> getAdapter() {
        return new CalendarListAdapter();
    }

    @Override
    public void loadData(int page) {
        getDatas(page);
    }
    private void getDatas(int page) {
//            .addParam("year", DateUtil.getCurrentYear() + "")
//                .addParam("monthday", DateUtil.getCurrentMonth() + "" + DateUtil.getCurrentDay())
        if (page == 0) {
            GetRequest request = ViseHttp.GET(Api.CJRL);
            request.addParam("year", DateUtil.getCurrentYear() + "");
            request.addParam("monthday", DateUtil.getCurrentMonth() + "" + DateUtil.getCurrentDay());
            request.request(new ACallback<BaseTResp<ArrayList<CalendarItem>>>() {
                @Override
                public void onSuccess(BaseTResp<ArrayList<CalendarItem>> data) {
                    if (data.data.size()>0){
                        FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(), data.data, null);
                    }

                }

                @Override
                public void onFail(int errCode, String errMsg) {
                    FastManager.getInstance().getHttpRequestControl().httpRequestError(getIHttpRequestControl(), new Throwable(errMsg));
                }
            });
        } else {
            FastManager.getInstance().getHttpRequestControl().httpRequestSuccess(getIHttpRequestControl(), new ArrayList<>(), null);
        }
    }
//    private void startUpFetch() {
//        mDefaultPage++;
//        mAdapter.setUpFetching(true);
//
//        loadData(mDefaultPage);
//    }
//    private void sendMessage(final String content) {
//        AVObject avObject = new AVObject(BussConstant.TABLE_ONLINE_SERVICE);
//        avObject.put(BussConstant.CONTENT, content);
//        avObject.put(BussConstant.USER, mUser);
//        avObject.put(BussConstant.IS_REPLY, false);
//        avObject.saveInBackground(new SaveCallback() {
//            @Override
//            public void done(AVException e) {
//                if (e == null) {
//                    etContent.setText("");
//                    DisplayUtils.hideSoftInput(mContext, etContent);
//                    mDefaultPage = 0;
//                    loadData(mDefaultPage);
//
//                    if (content.contains("您好") || content.contains("你好")) {
//                        autoReply("您好！我是客服‘客服妹子’,很高兴为您服务。");
//                    } else if (content.contains("在？") || content.contains("在?") || content.contains("在吗") || content.contains("在不")) {
//                        autoReply("在的哟,请问有什么能帮到您吗？");
//                    }
//                } else {
//                    ToastUtil.show("消息发送失败");
//                }
//            }
//        });
//    }
//    private void loadData(int page) {
//        mAdapter.setUpFetching(false);
//
//        AVObject user = AVObject.createWithoutData(BussConstant.TABLE_USER, mUser.getObjectId());
//
//        AVQuery<AVObject> query = new AVQuery<>(BussConstant.TABLE_ONLINE_SERVICE);
//        query.include(BussConstant.USER);
//        query.setLimit(mDefaultPageSize);
//        query.setSkip(page * mDefaultPageSize);
//        query.orderByDescending("createdAt");
//        query.whereEqualTo(BussConstant.USER, user);
//        query.findInBackground(new FindCallback<AVObject>() {
//            @Override
//            public void done(List<AVObject> list, AVException e) {
//                if (e == null) {
//                    List<OnLineServiceMsg> msgs = new ArrayList<>();
//                    LoggerManager.e("list:" + list.size());
//                    for (AVObject object : list) {
//                        boolean isReply = object.getBoolean(BussConstant.IS_REPLY);
//                        AVUser avUser = object.getAVUser(BussConstant.USER);
//                        String content = object.getString(BussConstant.CONTENT);
//                        long time = object.getCreatedAt().getTime();
//
//                        msgs.add(0, new OnLineServiceMsg(time, avUser, content, isReply));
//                    }
//                    //                    mAdapter.replaceData(msgs);
//
//
//                    if (mDefaultPage == 0) {
//                        mAdapter.replaceData(msgs);
//                        recyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
//                    } else {
//                        mAdapter.addData(0, msgs);
//                    }
//                    mAdapter.setUpFetchEnable(true);
//                } else {
//                    ToastUtil.show("获取数据出错,请检查网络后重试");
//                    mAdapter.setUpFetchEnable(true);
//                }
//            }
//        });
//    }
//    private void autoReply(String reply) {
//        AVObject avObject = new AVObject(BussConstant.TABLE_ONLINE_SERVICE);
//        avObject.put(BussConstant.CONTENT, reply);
//        avObject.put(BussConstant.USER, mUser);
//        avObject.put(BussConstant.IS_REPLY, true);
//        avObject.saveInBackground(new SaveCallback() {
//            @Override
//            public void done(AVException e) {
//                if (e == null) {
//                    mDefaultPage = 0;
//                    loadData(mDefaultPage);
//                }
//            }
//        });
//    }

}
